var name = localStorage.getItem('username')
name = JSON.parse(name)
var admin = document.querySelector('.top .right p')
admin.innerText = name

axios.interceptors.request.use( 
    (config)=>{  
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config;
    },
    (error)=>{
        return Promise.reject(error)
    }
)
axios.interceptors.response.use( 
    (res)=>{  
        return res;
    },
    (error)=>{ 
        if( error.response.status == 401 ){
            localStorage.removeItem('token');
            location.href = './register.html';
        }
        return Promise.reject(error)
    }
)

var tbody = document.querySelector('tbody')
let page = 1
let count = 4

var add = document.querySelector('.add')
var write = document.querySelector('#write')

add.onclick = () => {
    write.classList.add('write')
    write.innerHTML =
        `
        <h1>添加</h1>
        <input type="text" placeholder="岗位编号">
        <input type="text" placeholder="岗位编码">
        <input type="text" placeholder="岗位名称">
        <input type="text" placeholder="岗位排序">
        <button class="push">添加</button>
        `
}
write.onclick = (e) => {
    var time = new Date()
    console.log(time);
    if (e.target.classList == 'push') {
        var addinput = document.querySelectorAll('.write input')
        axios.post('/user/adduser', {
            number: addinput[0].value,
            code: addinput[1].value,
            name: addinput[2].value,
            no: addinput[3].value,
            time: time
        }).then((res) => {
            alert(res.data.mas)
            write.innerHTML = ''
            write.classList.remove('write')
        })
    }
}

function render(data) {
    data.map((item) => {
        tbody.innerHTML +=
            `
            <tr>
                <td><input type="checkbox"></td>
                <td>${item.number}</td>
                <td>${item.code}</td>
                <td>${item.name}</td>
                <td>${item.no}</td>
                <td>${item.status ? '正常' : '异常'}</td>
                <td>${item.time}</td>
                <td class="pop" data-name="${item.name}">删除</td>
            </tr>
            `
    })
}

var h5 = document.querySelector('h5')
var h6 = document.querySelector('h6')
function render_user (page , count){
    tbody.innerHTML = ''
    axios.get( '/user/render' , {params:{page:page , count : count}}).then((res) => {
        render(res.data.list)
        h5.innerText = `共 ${res.data.lengths} 条`
        h6.innerText = page
    })
}

render_user(page , count)

var last = document.querySelector('.last')

var next = document.querySelector('.next')

last.onclick = () => {
    if(page > 1){
        page--
        render_user(page,count)
    }
}
next.onclick = () => {
    if(page < 4){
        page++
        render_user(page,count)
    }
}

//删除
tbody.onclick = (e) => {
    if(e.target.classList == 'pop'){
        axios.post('/user/delete' , {name : e.target.dataset.name}).then((res) => {
            if(res.data.code == 200){
                render_user(res.data.num , count)
            }
        })
    }
}

//查询
var sousuo = document.querySelector('.sousuo input')
var mid = document.querySelector('.mid')

mid.onclick = () => {
    axios.get('/user/sousuo' , {params : {name : sousuo.value}}).then((res) => {
        render(res.data.list)
    })
}